package com.itany.conf;

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.configuration.ConfigurationKeys;
import org.jasig.cas.client.util.AbstractConfigurationFilter;
import org.jasig.cas.client.util.CommonUtils;
import org.jasig.cas.client.validation.Assertion;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.Principal;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class MyWrapperFilter  extends AbstractConfigurationFilter {
    private String roleAttribute;
    private boolean ignoreCase;

    public MyWrapperFilter() {
    }

    public void destroy() {
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        AttributePrincipal principal = this.retrievePrincipalFromSessionOrRequest(servletRequest);
        //principal.getName();
       // ApplicationCotextHold.getBean();
        Map<String, Object> map =  principal.getAttributes();
        map.put("mytest","1232131312");
        filterChain.doFilter(new MyWrapperFilter.CasHttpServletRequestWrapper((HttpServletRequest)servletRequest, principal), servletResponse);
    }

    protected AttributePrincipal retrievePrincipalFromSessionOrRequest(ServletRequest servletRequest) {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpSession session = request.getSession(false);
        Assertion assertion = (Assertion)((Assertion)(session == null ? request.getAttribute("_const_cas_assertion_") : session.getAttribute("_const_cas_assertion_")));
        return assertion == null ? null : assertion.getPrincipal();
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        super.init(filterConfig);
        this.roleAttribute = this.getString(ConfigurationKeys.ROLE_ATTRIBUTE);
        this.ignoreCase = this.getBoolean(ConfigurationKeys.IGNORE_CASE);
    }

    final class CasHttpServletRequestWrapper extends HttpServletRequestWrapper {
        private final AttributePrincipal principal;

        CasHttpServletRequestWrapper(HttpServletRequest request, AttributePrincipal principal) {
            super(request);
            this.principal = principal;
        }

        public Principal getUserPrincipal() {
            return this.principal;
        }

        public String getRemoteUser() {
            return this.principal != null ? this.principal.getName() : null;
        }

        public boolean isUserInRole(String role) {
            if (CommonUtils.isBlank(role)) {
                MyWrapperFilter.this.logger.debug("No valid role provided.  Returning false.");
                return false;
            } else if (this.principal == null) {
                MyWrapperFilter.this.logger.debug("No Principal in Request.  Returning false.");
                return false;
            } else if (CommonUtils.isBlank(MyWrapperFilter.this.roleAttribute)) {
                MyWrapperFilter.this.logger.debug("No Role Attribute Configured. Returning false.");
                return false;
            } else {
                Object value = this.principal.getAttributes().get(MyWrapperFilter.this.roleAttribute);
                if (value instanceof Collection) {
                    Iterator var3 = ((Collection)value).iterator();

                    while(var3.hasNext()) {
                        Object o = var3.next();
                        if (this.rolesEqual(role, o)) {
                            MyWrapperFilter.this.logger.debug("User [{}] is in role [{}]: true", this.getRemoteUser(), role);
                            return true;
                        }
                    }
                }

                boolean isMember = this.rolesEqual(role, value);
                MyWrapperFilter.this.logger.debug("User [{}] is in role [{}]: {}", new Object[]{this.getRemoteUser(), role, isMember});
                return isMember;
            }
        }

        private boolean rolesEqual(String given, Object candidate) {
            return MyWrapperFilter.this.ignoreCase ? given.equalsIgnoreCase(candidate.toString()) : given.equals(candidate);
        }
    }
}
